package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import dao.UserDAO;

import model.Grupo;
import model.User;

public class LoginAction extends BaseAction {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String username;
    private String password;
    private UserDAO userDAO = new UserDAO();
    
    public Boolean tienePermisos(User user){
		return true;
	}
    
    public String execute() throws Exception {
    	if (username != null){
    		System.out.println("busca el usuario "+username+" en la base de datos");
	    	User user = userDAO.findByUsernameOrDni(username);
	    	if ((user != null) && user.getPassword().equals(password)){
	    		System.out.println("usuario encontrado, se procede a crear la session");
				Map<String, Object> session = ActionContext.getContext().getSession();
				session.put("user",user);
				session.put("is_login","true");
	            session.put("context", new Date());
	            return SUCCESS;
	    	}
    	}
        return ERROR;
    }

    public String logout() throws Exception {
    	Map<String, Object> session = ActionContext.getContext().getSession();
    	session.remove("user");
    	session.remove("is_login");
        session.remove("context");
        System.out.println("se elimina la session y se produce el logout");
        return SUCCESS;
    }

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

   

}

